<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once(APPPATH . '/controllers/super_admin' . EXT);
class Student extends Super_admin {

    function __construct() {
        parent::__construct();

        $this->load->model(array(
            'User_model'
        ));

        $this->load->helper(array(
            'security',
            'string',
        ));
    }

    function index() {
        $access = array('admin'); //admin only
        $this->_is_authorize($access); //check super_admin controller

        $this->list_records();
    }
    public function home()
    {
       $access = array('student');
       $this->_is_authorize($access); //check super_admin controller

       $data['fullname'] = $this->User_model->get_fullname();

       $this->load->view('student/home', $data);

    }
    public function profile()
    {
        redirect('profile');
    }
    /**
     * list_records
     *
     * Load student list
     *
     * @access public
     * @param void
     * @return void
     */
    function list_records() {
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $this->load->view('student/list');
    }
    /**
     * get_students
     *
     * get data students for listing
     *
     * @access public
     * @param void
     * @return string
     */
    function get_students() {
        
        $where = array();
        $select = 'u.user_id, u.user_no,CONCAT(u.lname, ", ",u.fname) name, u.status';

        $page = $this->input->post('page');
        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        if ($this->input->post('sidx') == 'category')
            $order_by .= ', sub_category';

        $total = $this->User_model->get_total($where);
        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        $args = array(
                    'select' => $select,
                    'where' =>  'u.user_type = "student"',
                    'join' => array(),
                    'limit' => LIMIT,
                    'offset' => $offset,
                    'order_by' => $order_by,
                    'resource_id' => TRUE,
        );

        $records = $this->User_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'user_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }
    /**
     * add
     *
     * initialize add form
     *
     * @access public
     * @param void
     * @return void
     */
    function add(){
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $data = array(
            'user_id' => 0,
            'user_no' => '',
            'fname' => '',
            'mname' => '',
            'lname' => '',
            'address' => '',
            'email' => '',
            'contact_person' => '',
            'contact_no' => '',
            'status' => 'Active',
        );

        $data['courses_op'] = array(
            array('course_id' => 'BSIT', 'course_label' =>'BSIT'),
            array('course_id' => 'BSComTech', 'course_label' =>'BSComTech'
        ));
        
        $this->load->view('student/form', $data);
        
    }
    /**
     * edit
     *
     * view edit page
     *
     * @access public
     * @param int
     * @return void
     */
    function edit($id){
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $params = array(
                'select' => '*',
                'where' => array('user_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );

        $data = $this->User_model->get_record($params);

        $data['courses_op'] = array(
            array('course_id' => 'BSIT', 'course_label' =>'BSIT'),
            array('course_id' => 'BSComTech', 'course_label' =>'BSComTech'
        ));

        $this->load->view('student/form', $data);
        
        
    }
    /**
     * view
     *
     * display record
     *
     * @access public
     * @param int
     * @return void
     */
    function view($id){
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $params = array(
                'select' => '*',
                'where' => array('user_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );

        $data = $this->User_model->get_record($params);

        $this->load->view('student/display', $data);
        
        
    }
    /**
     * submit
     *
     * submit data
     *
     * @access public
     * @param void
     * @return void
     */
    function submit(){
        
        $response = FALSE;
        $user_id = $this->input->post('user_id', TRUE);

        $data = array(
            'fname' => $this->input->post('fname',TRUE),
            'mname' => $this->input->post('mname',TRUE),
            'lname' => $this->input->post('lname',TRUE),
            'course' => $this->input->post('course',TRUE),
            'user_no' => $this->input->post('user_no',TRUE),
            'email' => $this->input->post('email',TRUE),
            'address' => $this->input->post('address',TRUE),
            'contact_no' => $this->input->post('contact_no',TRUE),
            'contact_person' => $this->input->post('contact_person',TRUE),
            'status' => $this->input->post('status',TRUE),
            'user_type' => 'student'

        );


        if (empty($user_id)) {

            $id = $this->User_model->_insert($data);
            
            //send only once; uncomment next line if SMTP server have already been set up
            //$this->email_password($data['user_no'], $data['email'], $id);
            
            $msg = sprintf(lang('cms_add_success'), "Student");
            $response = TRUE;

        } else {
 
            $where = array('user_id' => $user_id);

            $this->User_model->_update($where, $data);

            $msg = sprintf(lang('cms_edit_success'), "Student");

            $response = TRUE;
        }

        _success($msg, FALSE);
        echo json_encode($response);
    }
    /**
     * set_to_inactive
     *
     * set status inactive
     *
     * @access public
     * @param int
     * @return boolean
     */
    function set_to_inactive($id) {
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        _success(sprintf(lang('cms_custom'), "Record sucessfully set to inactive."));

        
        $where = array('user_id' => $id);
        
        $data = array(
            'status' => 'Inactive'
        );
        
        $this->User_model->_update($where, $data);

        $msg = sprintf(lang('cms_custom'), "Record sucessfully set to inactive.");

        $response = TRUE;
        _success($msg, FALSE);
        echo json_encode($response);
    }
    /**
     * email_password
     *
     * Email user account details
     *
     * @access public
     * @param string
     * @param string
     * @param integer
     * @return boolean
     */
    function email_password($userno, $email, $user_id = 0)
    {
            $access = array('admin');
            $this->_is_authorize($access); //check super_admin controller

            $this->load->library('email');
            
            $this->email->from('admin@dummy-site.com', 'WebAdmin');
            $this->email->to($email);
            $this->email->subject('i-Teach Details: Account');

            $password = random_string('alnum', 8);
 

            $msg = "Hi!\n\nHere are your account details for ". SITE_TITLE ."\n\nUser no: ". $userno ."\nPassword: ". $password ."\n\nYou may login at ". site_url() .".";
            $msg .= "\n\n";
            $msg .= "You can change your password in account setting.";
            
            $this->email->message($msg);

            if ( ! $this->email->send())
            {
                    return FALSE;
            }

            $where = 'userno = "'. $userno .'"';
            if ($user_id) $where = 'user_id = '. $user_id;

            return $this->User_model->_update($where, array(
                    'password' => md5($password)
            ));
    }

}
/* End of file student.php */
/* Location: ./application/controllers/student.php */
